﻿using System;
using Android.Content;
using Android.InputMethodServices;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Java.Lang;

namespace LePcCtrlApp
{
    //接收键盘回调
    public interface ISoftKeyEventListener
    {
        void OnKeyDown(Android.Views.Keycode code);
        void OnKeyUp(Android.Views.Keycode code);
    }
   
    public class SoftKeyBoardView : LinearLayout, KeyboardView.IOnKeyboardActionListener
    {

        private ISoftKeyEventListener _inputEvtListener;

        public SoftKeyBoardView(Context context, IAttributeSet attrs) : base(context, attrs)
        {
            Init(context);
        }

        private void Init(Context context)
        {
            //Orientation = Orientation.Vertical;
            LayoutInflater.From(context).Inflate(Resource.Layout.KeyViewLayout, this, true);

            KeyboardView kv = (KeyboardView)FindViewById(Resource.Id.soft_keyboard);
            Keyboard charKey = new Keyboard(context, Resource.Xml.CharSoftKeyBoard);
            kv.Keyboard = charKey;
            kv.Enabled = true;
            kv.PreviewEnabled = true;
            kv.OnKeyboardActionListener = this;
        }

        // 设置接受字符的EditText
        public void SetKeyEventReceiver(ISoftKeyEventListener inputListener)
        {
            _inputEvtListener = inputListener;
        }

        public void OnKey([GeneratedEnum] Android.Views.Keycode primaryCode, [GeneratedEnum] Android.Views.Keycode[] keyCodes)
        {
            //throw new NotImplementedException();
        }

        public void OnPress([GeneratedEnum] Android.Views.Keycode primaryCode)
        {            
            if (_inputEvtListener!=null)
            {
                _inputEvtListener.OnKeyDown(primaryCode);
            }
        }

        public void OnRelease([GeneratedEnum] Android.Views.Keycode primaryCode)
        {
            if (_inputEvtListener != null)
            {
                _inputEvtListener.OnKeyUp(primaryCode);
            }
        }

        public void OnText(ICharSequence text)
        {
            //throw new NotImplementedException();
        }

        public void SwipeDown()
        {
            //throw new NotImplementedException();
        }

        public void SwipeLeft()
        {
            //throw new NotImplementedException();
        }

        public void SwipeRight()
        {
            //throw new NotImplementedException();
        }

        public void SwipeUp()
        {
            //throw new NotImplementedException();
        }
    }


}